package com.petmall.controller;

import com.petmall.entity.*;
import com.petmall.service.ItemService;
import com.petmall.util.SessionAttributes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by pig2guang on 2017/4/10.
 */
@Controller
public class ItemController {

    @Autowired
    private ItemService itemservice;

    @RequestMapping(value = "/items")
    public String showAllItems(Model model){
        List<Item> items = itemservice.showAllItems();
        //System.out.print(items.size());
        model.addAttribute("items", items);
        model.addAttribute("items_category", "全部商品");
        return "shop-full-width";
    }

    @RequestMapping(value = "/items/category{item_category}")
    public String showClassifiedItems(@PathVariable int item_category, Model model){
        List<Item> items = itemservice.showClassifiedItems(item_category);
        //System.out.print(items.size());
        model.addAttribute("items", items);
        model.addAttribute("items_category", itemservice.getItemCategory(item_category));
        return "shop-full-width";
    }

    @RequestMapping(value = "/singleItem/{item_id}")
    public String showSingleItem(@PathVariable long item_id, Model model){
        //System.out.print(item_id);
        Item item = itemservice.selectItemById(item_id);
        List<Review> reviews = itemservice.listItemReviews(item_id);
        int reviews_num = reviews.size();
        //System.out.print(reviews_num);
        model.addAttribute("item", item);
        model.addAttribute("reviews", reviews);
        model.addAttribute("reviews_num", reviews_num);
//        Cart cart = new Cart();
//        model.addAttribute("cart",cart);
        return "variable-product-page";
    }

    @RequestMapping(value = "/itemManagement")
    public String manageAllItems(HttpSession httpSession, Model model){
        if(httpSession.getAttribute(new SessionAttributes().admin) != null){
            List<Item> items = itemservice.showAllItems();
            //System.out.print(items.size());
            model.addAttribute("items", items);
            model.addAttribute("item", new Item());
            return "item-management";
        }else{
            model.addAttribute("admin", new Admin());
            model.addAttribute("user", new User());
            return "login";
        }
    }

    @RequestMapping(value = "/addItem")
    public String loginCheck(@ModelAttribute(value = "item") Item item, HttpSession httpSession,Model model){
        if (httpSession.getAttribute(new SessionAttributes().admin) != null) {
            itemservice.addItem(item);
            List<Item> items = itemservice.showAllItems();
            //System.out.print(items.size());
            model.addAttribute("items", items);
            model.addAttribute("item", new Item());
            return "item-management";
        } else {
            model.addAttribute("admin", new Admin());
            model.addAttribute("user", new User());
            return "login";
        }
    }

    @RequestMapping(value = "/itemInfo", method = RequestMethod.GET)
    public String itemInfo(@RequestParam("item_id") String item_id, HttpSession httpSession, Model model) {
        if (httpSession.getAttribute(new SessionAttributes().admin) != null) {
            model.addAttribute("item", itemservice.selectItemById(Long.valueOf(item_id)));
            return "item-info";
        } else {
            model.addAttribute("admin", new Admin());
            model.addAttribute("user", new User());
            return "login";
        }
    }

    @RequestMapping(value = "/modifyItemInfo")
    public String modifyItemInfo(@ModelAttribute(value = "item") Item item, HttpSession httpSession, Model model){
        if (httpSession.getAttribute(new SessionAttributes().admin) != null) {
            itemservice.modifyItemInfo(item);
            model.addAttribute("item", itemservice.selectItemById(item.getItem_id()));
            return "item-info";
        } else {
            model.addAttribute("admin", new Admin());
            model.addAttribute("user", new User());
            return "login";
        }
    }


}
